//
//  GHTabBarController.m
//  12-guhu_weibo
//
//  Created by GHapple on 15-1-31.
//  Copyright (c) 2015年 itcast. All rights reserved.
//

#import "GHTabBarController.h"
#import "GHHomeController.h"
#import "GHMssageController.h"
#import "GHDIscoverController.h"
#import "GHProfileController.h"
#import "GHTabbar.h"
#import "GHNavigationController.h"


@interface GHTabBarController ()<GHtabbardelegate>
@property(nonatomic,strong)GHTabbar *mytabbar;
@end

@implementation GHTabBarController

// 这个方法在调用init方法之前会调用,可以在此方法中添加子控制器
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        // 调用自己的添加控制器的方法,提供标题
        UIViewController *home = [self addControllerWithClass:[GHHomeController class] title:@"首页" image:@"tabbar_home" selectedImage:@"tabbar_home_selected"];
        UIViewController *message = [self addControllerWithClass:[GHMssageController class] title:@"消息" image:@"tabbar_message_center" selectedImage:@"tabbar_message_center_selected"];
        UIViewController *discover = [self addControllerWithClass:[GHDIscoverController class] title:@"发现" image:@"tabbar_discover" selectedImage:@"tabbar_discover_selected"];
        UIViewController *profile = [self addControllerWithClass:[GHProfileController class] title:@"我" image:@"tabbar_profile" selectedImage:@"tabbar_profile_selected"];
        
    }
    return self;
}


- (void)viewDidLoad {
    [super viewDidLoad];
     //  自定义tabbar
    GHTabbar *mytabbar = [[GHTabbar alloc]init];
    mytabbar.frame = self.tabBar.frame;
    [self.view addSubview:mytabbar];
//    mytabbar.backgroundColor = [UIColor redColor];
    
    [self.tabBar removeFromSuperview];
    self.mytabbar = mytabbar;
    mytabbar.delegate = self;
    
}

#pragma mark  代理方法
- (void)tabbar:(GHTabbar *)tabbar from:(NSUInteger)from to:(NSUInteger)to{
    self.selectedIndex = to;

}

// 不是所有的控制器都是init方法出来的

- (UIViewController *)addControllerWithClass:(Class)class  title:(NSString *)title image:(NSString *)image selectedImage:(NSString *)selectedImage{
    UIViewController *vc = [[class alloc]init];
    return  [self addControllerWithVc:vc title:title image:image selectedImage:selectedImage];
}
- (UIViewController *)addControllerWithVc:(UIViewController *)vc  title:(NSString *)title image:(NSString *)image selectedImage:(NSString *)selectedImage{

    vc.title = title;
    // 如果是IOS的话要告诉图片不要渲染
    vc.tabBarItem.image = [UIImage imageNamed:image];
    UIImage *seleImage = [UIImage imageNamed:selectedImage];
    seleImage = [seleImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    // 设置选中图片
    vc.tabBarItem.selectedImage  = seleImage;
    // 设置背景颜色
//    vc.view.backgroundColor  = GHRandomColor;
    // 设置选中的字体的颜色
    [vc.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor orangeColor]} forState:UIControlStateSelected];
////    包装成导航控制器
    GHNavigationController *nv = [[GHNavigationController alloc]initWithRootViewController:vc];
    [self.mytabbar additem:vc.tabBarItem];
    [self addChildViewController:nv];

    return vc;
}
@end